package com.example.mbl.service.impl;

import com.example.mbl.dao.ResearchMapper;
import com.example.mbl.dto.ResearchArticleDto;
import com.example.mbl.entity.Research;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.ArticleResult;
import com.example.mbl.result.business.ResearchPageResult;
import com.example.mbl.result.business.ResearchResult;
import com.example.mbl.service.ResearchService;
import com.example.mbl.util.Tools;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResearchServiceImpl implements ResearchService {
    @Autowired
    private ResearchMapper researchMapper;

    @Override
    public WebRestResult addResearch(Research research) {
        WebRestResult result = new WebRestResult();
        research.setResearchid(UUIDUtil.randomUUID());
        if(Tools.isEmpty(research.getFlag()))
            research.setFlag("0");
        research.setCreatedate(new Date());
        if(researchMapper.insert(research)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增课题失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteResearch(Research research) {
        WebRestResult result = new WebRestResult();
        if(researchMapper.deleteByPrimaryKey(research.getResearchid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除课题失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateResearch(Research research) {
        WebRestResult result = new WebRestResult();
        if(researchMapper.updateByPrimaryKeySelective(research)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新课题失败");
        }
        return result;
    }

    @Override
    public WebRestResult setResearchVisible(Research research) {
        WebRestResult result = new WebRestResult();
        if(researchMapper.setResearchVisible(research.getResearchid())==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("设置失败");
        }
        return result;
    }

    @Override
    public PageResult<ResearchPageResult> getResearchList(String name, int page, int pagesize) {
        PageResult<ResearchPageResult> result = new PageResult<>();
        int start = (page-1) * pagesize;

        List<ResearchPageResult> list = researchMapper.getResearchList(name,start,pagesize);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public ResearchResult getResearchItem(String researchid) {
        ResearchResult result = researchMapper.getResearchItem(researchid);
        if(!Tools.isEmpty(result)){
            List<ArticleResult> list = researchMapper.getArticlesByResearchid(researchid);
            if(list.size()>0){
                result.setArticleResultList(list);
            }
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取课题详情失败");
        }
        return result;
    }

    @Override
    public WebRestResult addResearchArticle(ResearchArticleDto researchArticleDto) {
        WebRestResult result = new WebRestResult();
        if(researchArticleDto.getArticleList().size()==0){
            result.setResult(WebRestResult.SUCCESS);
            return result;
        }
        else{
            if(researchMapper.addResearchArticle(researchArticleDto)==researchArticleDto.getArticleList().size()){
                result.setResult(WebRestResult.SUCCESS);
            }
            else{
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("添加文章失败");
            }
        }
        return result;
    }

    @Override
    public WebRestResult deleteResearchArticle(int id) {
        WebRestResult result = new WebRestResult();
        if(researchMapper.deleteResearchArticle(id)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败");
        }
        return result;
    }
}
